<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="请输入创意标签" :visible.sync="dialogVisible" @close="resetForm('batchAddIndustryRef')" width="600px">
        <el-form label-position="top" label-width="auto" size="medium" :hide-required-asterisk='true' :model="batchAddIndustryForm" :rules="batchAddIndustryRules" ref="batchAddIndustryRef" v-loading="loading">
            <el-form-item label="" prop="industry" style="margin-bottom:0">
                <div class="font_13 color_03">最多20个标签，每个不超过10个字，可用按Enter换行隔开： {{hasCheck.length}}/{{20-maxLength}}</div>
                <el-input type="textarea" ref="textArea" :rows="8" placeholder="请输入标签内容，用换行或者空格隔开" @input="getText" v-model="batchAddIndustryForm.industry">
                </el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="resetForm('batchAddIndustryRef')" class="el-button-width" size="small">取 消</el-button>
            <el-button type="primary" @click="submitForm('batchAddIndustryRef')" class="el-button-width" size="small">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            disabled: false,
            dialogVisible: false,
            hasCheck:[],
            maxLength:0,
            batchAddIndustryForm: {
                industry: "",
            },
            batchAddIndustryRules: {
                industry: [
                    {
                        required: true,
                        message: "请输入标签",
                        trigger: ["blur", "change"],
                    },
                ],
            },
        };
    },

    created() {
        this.$bus.on("batchAddIndustry", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.maxLength = this.$parent.createform.industryClass.ad_keywords.length
        });
    },
    beforeDestroy() {
        this.$bus.off("batchAddIndustry");
    },
    methods: {
        getText(){
            let arr = this.batchAddIndustryForm.industry.replace(/\r\n/g, '|').replace(/\n/g, '|').replace(/\s/g, '|')
            let text = arr.split('|').filter(Boolean)
            this.hasCheck = text
        },
        
        submitForm(formName){
            let is_check = false
            for(let i in this.hasCheck){
                if(this.hasCheck[i].length>10){
                   is_check = true
                   break
                }
            }
            if(this.hasCheck.length>(20-this.maxLength)){
                this.$message.error('标签最多只能添加20个！');
                return
            }else if(is_check){
                this.$message.error('标签长度不能超过10个字！');
                return
            }else{
                let oldData = this.$parent.createform.industryClass.ad_keywords
                let newArr = oldData.concat(this.hasCheck.filter(Boolean))
                this.$parent.createform.industryClass.ad_keywords = [...new Set(newArr)]
                this.resetForm(formName)
            }
            this.$parent.sessionSave()
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },
    },
};
</script>
<style scoped>
</style>

